//
//  YSUtil.m
//  luGongPro
//
//  Created by 王庆学 on 2017/8/23.
//  Copyright © 2017年 王庆学. All rights reserved.
//

#import "YSUtil.h"

typedef enum YSizeType
{
    YSizeTypeNone = 0,
    YSizeType3_5,
    YSizeType4_0,
    YSizeType4_7,
    YSizeType5_5,
    YSizeType9_7,
} YSizeType;

@implementation YSUtil

+ (BOOL)isViewController:(id)obj{
    if ([obj isKindOfClass:[UIViewController class]] &&
        ![obj isKindOfClass:[UINavigationController class]] &&
        ![obj isKindOfClass:[UITabBarController class]]){
        return YES;
    }
    return NO;
}

+ (YSizeType)getCurrentSizeType{
    static YSizeType currentSizeType = YSizeTypeNone;
    if (currentSizeType == YSizeTypeNone){
        if (ISIPHONE3_5){
            currentSizeType = YSizeType3_5;
        }
        else if (ISIPHONE4_0){
            currentSizeType = YSizeType4_0;
        }
        else if (ISIPHONE4_7){
            currentSizeType = YSizeType4_7;
        }
        else if (ISIPHONE5_5){
            currentSizeType = YSizeType5_5;
        }
        else if (ISIPHONE9_7){
            currentSizeType = YSizeType9_7;
        }
    }
    return currentSizeType;
}

+ (NSDictionary *)multiplicative
{
    return @{@0:@0,
             @1:@320,
             @2:@320,
             @3:@375,
             @4:@414,
             @5:@768};
}

+ (CGFloat)getLenght:(CGFloat)lenght{
    return [YSUtil getLenghtWithSizeType:YSizeType4_7 andLenght:lenght];
}

+ (CGFloat)getLenghtWithSizeType:(YSizeType)sizeType andLenght:(CGFloat)length
{
    CGFloat resultLength = length * [[self multiplicative][@([self getCurrentSizeType])] doubleValue] / [[self multiplicative][@(sizeType)] doubleValue];
    resultLength = ceil(resultLength);
    return resultLength;
}

+ (CGSize )sizeForWidth:(CGFloat)width andTextSize:(CGFloat)fontSize content:(NSString *)content defaultHeight:(CGFloat)defaultHeight{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],
                                 NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGSize labelSize = [content boundingRectWithSize:CGSizeMake(width, 1999) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    labelSize.height = ceil(labelSize.height) > defaultHeight ? ceil(labelSize.height) : defaultHeight ;
    labelSize.width = ceil(labelSize.width);
    return labelSize;
}

#pragma mark - 偏好类信息存储
+ (void)setObj:(NSObject *)obj key:(NSString *)key{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:obj forKey:key];
    [defaults synchronize];
}

+ (NSObject *)objForKey:(NSString *)key{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSObject *obj =(NSObject *) [defaults objectForKey:key];
    return obj;
}

+ (void)removeObjForKey:(NSString *)key{
    [self setObj:nil key:key];
}

@end
